package com.open.demo.span

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.text.style.ImageSpan

/**
 *
 * @Author: hailong.qiu hailong.qiu@xgimi.com
 * @Maintainer: hailong.qiu hailong.qiu@xgimi.com
 * @Date: 2021/7/2
 * @Copyright: 2021 www.xgimi.com Inc. All rights reserved.
 */
class NewImageSpan(
    drawable: Drawable,
    val moveX: Int = 0, val moveY: Int = 0,
    verticalAlignment: Int = ALIGN_CENTER
) : ImageSpan(drawable) {

    override fun draw(canvas: Canvas, text: CharSequence?, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
        val d = drawable
        canvas.save()
        val fm = paint.fontMetricsInt
        var transY:Float = (((y + fm.descent + y + fm.ascent) - (d.bounds.bottom))/2).toFloat()
        if (verticalAlignment == ALIGN_MID) {
        }
        canvas.translate(x + moveX, transY + moveY);
        d.draw(canvas)
        canvas.restore()
    }

    override fun getSize(paint: Paint, text: CharSequence?, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int {
        return super.getSize(paint, text, start, end, fm)
    }

    companion object {
        const val ALIGN_MID = 250
    }
}